package com.ivc.info.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cs.framework.common.util.CommonUtils;
import com.cs.framework.jdbc.common.dao.CommonDao;
import com.ivc.info.dto.base.SupplierDto;
import com.ivc.info.service.SupplierService;

/**
 * 供应商
 * 
 * @author hedongzhou
 * 
 */
@Service("supplierService")
public class SupplierServiceImpl implements SupplierService {

	@Resource
	private CommonDao commonDao;

	/**
	 * 查询供应商
	 * 
	 * @param supplierNo
	 *            供应商编号
	 * @return
	 */
	public SupplierDto get(String supplierNo) {
		return commonDao.getById(SupplierDto.class, supplierNo);
	}

	/**
	 * 查询供应商总数
	 * 
	 * @param supplierDto
	 * @return
	 */
	public int count(SupplierDto supplierDto) {
		return commonDao.count("Supplier.query", supplierDto);
	}

	/**
	 * 查询供应商列表
	 * 
	 * @param supplierDto
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public <T extends SupplierDto> List<T> query(T supplierDto) {
		return (List<T>) commonDao.selectList("Supplier.query", supplierDto, supplierDto, supplierDto.getClass());
	}

	/**
	 * 获取供应商名称
	 * 
	 * @param supplierNo
	 *            供应商编号
	 * @return
	 */
	public String getSupplierName(String supplierNo) {
		SupplierDto supplierDto = get(supplierNo);

		if (supplierDto != null) {
			return supplierDto.getName();
		} else {
			return null;
		}
	}

	/**
	 * 新增供应商
	 * 
	 * @param supplierDto
	 * @return
	 */
	public SupplierDto save(SupplierDto supplierDto) {
		SupplierDto entity = get(supplierDto.getSupplierNo());
		if (entity != null) {
			CommonUtils.throwRun("该供应商编号【" + supplierDto.getSupplierNo() + "】已存在");
		}

		return commonDao.save(supplierDto);
	}

	/**
	 * 修改供应商
	 * 
	 * @param supplierDto
	 * @return
	 */
	public SupplierDto update(SupplierDto supplierDto) {
		SupplierDto entity = get(supplierDto.getSupplierNo());
		if (entity == null) {
			CommonUtils.throwRun("未查询到相关的供应商信息");
		}

		CommonUtils.copyBoValue(entity, supplierDto);

		return commonDao.update(entity);
	}

}
